// 从pinia引入小仓库
import { reqC1List, reqC2List, reqC3List } from "@/api/product/attr/attr";
import { defineStore } from "pinia";
import type { categoryStoreData } from "./interface/category";

let useCategoryStore = defineStore('category', {
    // 存储数据
    state: ():categoryStoreData => {
        return {
            C1List:[],
            C2List:[],
            C3List:[],
            // 将一级分类的id存到仓库中,初始值设置为空
            c1Id:'',
            c2Id:'',
            c3Id:''
        }
    },
    // 处理业务,调用方法的地方
    actions: {
        // 获取一级分类的方法
        async getC1List() {
            let result = await reqC1List()
            this.C1List = result
        },
        async getC2List(){
            // 处理小bug,当一级分类的id改变时,二级和三级的应该清空
            // 清空id以及列表
            this.c2Id = ''
            this.c3Id = ''
            this.C2List = []
            this.C3List = []
            let result = await reqC2List(this.c1Id)
            this.C2List = result
        },
        async getC3List(){
            // 当二级分类的id改变时,三级应该清空
            // 清空id以及列表
            this.c3Id = ''
            this.C3List =[]
            let result = await reqC3List(this.c2Id)
            this.C3List = result
        }
    }
})

// 对外暴露仓库
export default useCategoryStore